//
//  QQCharge.m
//  EasyToo
//
//  Created by linshengqin on 13-8-31.
//
//

#import "QQChargeView.h"
#import "QQChargeConfirmViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "DeviceUtil.h"

#define SCREEN_HEIGHT [DeviceUtil getScreenHeight]
@implementation QQChargeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //[self setBackgroundColor:[UIColor whiteColor]];
        [self setBackgroundColor:BackgroundColor];
        qq_model = [QQModel sharedata];

    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self initScrollView];
    [self initView];
}

-(void)initScrollView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}


-(void) initView
{
  //  [super viewDidLoad];
    
 //   [self.navigationItem setTitle:@"Q币"];
    
    
    UILabel *label_qq = [[[UILabel alloc]initWithFrame:CGRectMake(30, 20.0f/480.0f*SCREEN_HEIGHT, 180, 30)]autorelease];
    label_qq.text = @"填写QQ信息:";
    [label_qq setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_qq];
    
    UIImage *image = [UIImage imageNamed:@"select_button"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    imageView.frame = CGRectMake(20, 30.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView];
    
    self.qqNumber = [[[UITextField alloc] initWithFrame:CGRectMake(30, 60.0f/480.0f*SCREEN_HEIGHT, 250, 30)]autorelease];
    [self.qqNumber setBorderStyle:UITextBorderStyleBezel];
    self.qqNumber.borderStyle = UITextBorderStyleRoundedRect;
    self.qqNumber.layer.borderWidth = 1 ;
    self.qqNumber.layer.cornerRadius = 10 ;
    self.qqNumber.placeholder = @"QQ帐号";
    [_bgScrollView addSubview:self.qqNumber];
    self.qqNumber.delegate = self;
    self.qqNumber.keyboardType = UIKeyboardTypeNumberPad;
    self.qqNumber.returnKeyType = UIReturnKeyDone;
    
    
    UILabel *label_value = [[[UILabel alloc]initWithFrame:CGRectMake(30, 100.0f/480.0f*SCREEN_HEIGHT, 180, 30)]autorelease];
    label_value.text = @"请选择充值面额:";
    [label_value setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_value];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image] ;
    imageView2.frame = CGRectMake(20, 110.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView2];
    
    text_value = [[[UITextField alloc] initWithFrame:CGRectMake(30, 140.0f/480.0f*SCREEN_HEIGHT, 250, 30)]autorelease];
    [text_value setBorderStyle:UITextBorderStyleBezel];
    text_value.borderStyle = UITextBorderStyleRoundedRect;
    text_value.layer.borderWidth = 1 ;
    text_value.layer.cornerRadius = 10 ;
    [text_value setDelegate:self];
    
    [_bgScrollView addSubview:text_value];
    // 建立 UIToolbar
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancelPicker)];
    // 把按鈕加進 UIToolbar
    toolBar.items = [NSArray arrayWithObject:right];
    text_value.inputAccessoryView = toolBar;
    
    pickerArray = [[NSArray arrayWithObjects:@"5",@"10",@"20",@"30",nil] retain];
    selectPicker = [[[UIPickerView alloc]initWithFrame:CGRectZero]autorelease];
    selectPicker.showsSelectionIndicator = YES;
    selectPicker.frame = CGRectMake(0, 100.0f/480.0f*SCREEN_HEIGHT, 0, 0);
    
    text_value.inputAccessoryView = toolBar;
    text_value.delegate = self;
    selectPicker.delegate = self;
    selectPicker.dataSource = self;
    selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    text_value.inputView = selectPicker;
    
    UILabel *label_pay = [[[UILabel alloc]initWithFrame:CGRectMake(30, 170.0f/480.0f*SCREEN_HEIGHT, 180, 30)]autorelease];
    label_pay.text = @"选择支付方式:";
    [label_pay setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_pay];
    
    UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image] ;
    imageView3.frame = CGRectMake(20, 180.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView3];
    
//    self.zfbButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 210.0f/480.0f*SCREEN_HEIGHT, 100, 30)];
//    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb.png"] forState:UIControlStateNormal];
//    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zhifubao2.png"] forState:UIControlStateSelected];
//    //[self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb"] forState:UIControlStateNormal];
//    [self.zfbButton addTarget:self action:@selector(zfbButtonPick:) forControlEvents:UIControlEventTouchDown];
//    [_bgScrollView addSubview:self.zfbButton];
    
    self.ylButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 210.0f/480.0f*SCREEN_HEIGHT, 100, 30)];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian1"] forState:UIControlStateNormal];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian2"] forState:UIControlStateSelected];
    [self.ylButton addTarget:self action:@selector(ylButtonPick:) forControlEvents:UIControlEventTouchDown];
    [_bgScrollView addSubview:self.ylButton];
    
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 260.0f/480.0f*SCREEN_HEIGHT, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"button_3"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    [_bgScrollView addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];

}

// 按下完成鈕後的 method
-(void) cancelPicker {
    [text_value endEditing:YES];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - UITextFieldDelegate

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    [self.qqNumber resignFirstResponder];
    
}
-(void)confirmMessge:(id)sender
{
    QQChargeConfirmViewController  *confirmView = [[[QQChargeConfirmViewController alloc] init]autorelease];
    [qq_model.account setString:self.qqNumber.text];
    [qq_model.price setString:text_value.text];
    [self.fatherView.navigationController pushViewController:confirmView animated:YES];
}



#pragma picker delegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [pickerArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [pickerArray objectAtIndex:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[[UILabel alloc] init]autorelease];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        //        pickerLabel.minimumFontSize =100.;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSInteger row = [selectPicker selectedRowInComponent:0];
    text_value.text = [pickerArray objectAtIndex:row];
}

- (IBAction)selectButton:(id)sender {
    [text_value endEditing:YES];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	return 100.0;
}

-(void)zfbButtonPick:(id)sender
{
    [self.zfbButton setSelected:YES];
    [self.ylButton setSelected:NO];
    qq_model.payType = 0;
}



-(void)ylButtonPick:(id)sender
{
    [self.zfbButton setSelected:NO];
    [self.ylButton setSelected:YES];
    qq_model.payType = 1;
}



@end
